﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HustQQModel;

namespace HustQQClient.UI
{
    public partial class ContactDisplay : UserControl
    {
        List<User> contacts;
        List<string> groupInfo;
        List<bool> isOpened;
        List<string> teams;

        public delegate SingleChat EnterEventHandler(string name, string id, string note, Image face);
        public event EnterEventHandler BeginChat;
        public delegate TeamChat TeamEventHandler(string id);
        public event TeamEventHandler BeginTeam;

        public ContactDisplay()
        {
            InitializeComponent();
        }

        #region predicated
        //public void ShowContacts()
        //{
        //    int gpos, upos;
        //    contactLayout.Controls.Clear();
        //    for (gpos = 0, upos = 0; gpos < groupInfo.Count; gpos++)
        //    {
        //        GroupInfoBlock gb = new GroupInfoBlock();
        //        gb.Click += GroupInfoBlock_Click;
        //        gb.GroupName = groupInfo[gpos];
        //        gb.Close();
        //        contactLayout.Controls.Add(gb);
        //        if (isOpened[gpos])
        //        {
        //            for (; upos < contacts.Count; upos++)
        //            {
        //                int s = contacts[upos].group.CompareTo(groupInfo[gpos]);
        //                if (s == 1)
        //                    break;
        //                else if (s == 0)
        //                {
        //                    User u = contacts[upos];
        //                    ContactInfoBlock blk = new ContactInfoBlock();
        //                    blk.Nickname = u.name;
        //                    blk.Note = u.note;
        //                    blk.State = u.online ? "在线" : "离线";
        //                    blk.FaceImg = new Bitmap("Penguins.jpg");
        //                    blk.ID = u.qq_ID;
        //                    blk.BeginChat += new ContactInfoBlock.EnterEventHandler(blk_BeginChat);
        //                    contactLayout.Controls.Add(blk);
        //                }
        //            }
        //        }
        //    }
        //}

        //public void GroupInfoBlock_Click(object sender, EventArgs e)
        //{
        //    string groupName = (sender as GroupInfoBlock).GroupName;
        //    for (int i = 0; i < groupInfo.Count; i++)
        //        if (groupInfo[i].CompareTo(groupName) == 0)
        //        {
        //            isOpened[i] = !isOpened[i];
        //            break;
        //        }
        //    ShowContacts_Simple();
        //}
        #endregion

        #region SingleChat
        public void UpdateContacts(List<User> con)
        {
            if (con != null)
                contacts = new List<User>(con);
            else
                contacts = new List<User>();
            contacts.Sort(new Comparison<User>((User u1, User u2) =>
                {
                    if (u1.group == u2.group && u1.online == u2.online)
                        return (u1.name.CompareTo(u2.name));
                    else if (u1.group == u2.group)
                        return (u2.online.CompareTo(u1.online));
                    return (u1.group.CompareTo(u2.group));
                }));
        }

        public void UpdateGroupsAndContacts(List<User> con)
        {
            UpdateContacts(con);
            groupInfo = new List<string>();
            isOpened = new List<bool>();groupInfo.Add(contacts[0].group);
            isOpened.Add(false);
            for (int i = 1; i < contacts.Count; i++)
                if (contacts[i].group.CompareTo(contacts[i - 1].group) != 0)
                {
                    groupInfo.Add(contacts[i].group);
                    isOpened.Add(false);
                }
        }

        void blk_BeginChat(string name, string id, string note, Image face)
        {
            if (BeginChat != null)
                BeginChat(name, id, note, face);
        }

        public void ShowContacts_Simple()
        {
            contactLayout.Controls.Clear();
            foreach (User u in contacts)
            {
                ContactInfoBlock blk = new ContactInfoBlock();
                blk.Nickname = u.name;
                blk.Note = u.note;
                blk.State = u.online ? "在线" : "离线";
                blk.FaceImg = InfoControl.FaceList[u.face];
                blk.ID = u.qq_ID;
                blk.BeginChat += new ContactInfoBlock.EnterEventHandler(blk_BeginChat);
                contactLayout.Controls.Add(blk);
            }
        }
        #endregion

        #region TeamChat
        public void ShowTeams(List<string> tm)
        {
            if (tm != null)
                teams = new List<string>(tm);
            else
                teams = new List<string>();
            teams.Sort();
            contactLayout.Controls.Clear();
            foreach (string t in teams)
            {
                GroupInfoBlock blk = new GroupInfoBlock();
                blk.GroupId = t;
                blk.GroupName = "";
                blk.BeginTeam += new GroupInfoBlock.EnterEventHandler(blk_BeginTeam);
                contactLayout.Controls.Add(blk);
            }
            
        }

        void blk_BeginTeam(string teamId)
        {
            if (BeginTeam != null)
                BeginTeam(teamId);
        }
        #endregion

    }
}
